/*
 * PanelLevelSelector.java
 *
 * Created on 24 December 2007, 13:30
 *
 * Copyright (C) 2007-2008  Hylke van der Schaaf
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, in version 3 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
package hextd.UI;

import hextd.HexTD;
import hextd.core.GameMap;
import hextd.core.MapLoaderClient;
import hextd.core.ContextNormal;
import hextd.message.MessageSettings;
import hextd.core.MapInfo;
import hextd.server.PlayerInfo;
import hextd.util.MapListener;
import hextd.util.SettingChangedListener;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.swing.DefaultListModel;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.StyleSheet;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;

/**
 *
 * @author  hylke
 */
public class PanelLevelSelector extends javax.swing.JPanel implements SettingChangedListener, MapListener {

	private ContextNormal context;
	private HexTD mainApp;
	private ArrayList<MapInfo> levelsSp;
	private ArrayList<MapInfo> levelsMp;
	private ArrayList<MapInfo> levels;
	private DefaultListModel listModel;
	private HTMLEditorKit editorkit;
	private boolean loading = false;
	private int waitingForMap = -1;
	private MapLoaderClient mapLoader;
	private Map<MapInfo, TreeNode> nodes = new HashMap<MapInfo, TreeNode>();

	/** Creates new form PanelLevelSelector */
	public PanelLevelSelector() {
		this.setVisible(false);
		this.listModel = new DefaultListModel();
		this.levelsSp = new ArrayList<MapInfo>();
		this.levelsMp = new ArrayList<MapInfo>();
		this.editorkit = new HTMLEditorKit();
	}

	public void setContext(ContextNormal c, HexTD mainApp, MapLoaderClient mapLoader) {
		this.context = c;
		this.mainApp = mainApp;
		this.mapLoader = mapLoader;
		mapLoader.addMapListener(this);

		initComponents();

		MapListTreeCellRenderer cellRenderer = new MapListTreeCellRenderer(context.getNetwork().getLocalPlayerInfo());
		jTreeLevels.setCellRenderer(cellRenderer);

		cellRenderer.setBackground(getBackground());
		cellRenderer.setBackgroundNonSelectionColor(context.colors.getColorBackGround());
		cellRenderer.setBackgroundSelectionColor(context.colors.getColorBackGroundDark());

		cellRenderer.setForeground(context.colors.getColorForeGround());
		cellRenderer.setTextNonSelectionColor(context.colors.getColorForeGround());
		cellRenderer.setTextSelectionColor(context.colors.getColorForeGroundPale());
		cellRenderer.setTextDisabledColor(context.colors.getColorForeGroundPale());

		StyleSheet s = this.context.loadStyleSheet("styles.css");
		editorkit.setStyleSheet(s);
		jTextPane_levelInfo.setEditorKit(this.editorkit);
		context.addSettingsListener(this);
	}

	@Override
	public void setVisible(boolean aFlag) {
		if (aFlag) {
			if (!isVisible()) {
				context.cleanContext();
				refillList();
			}
		}
		super.setVisible(aFlag);
	}

	@Override
	public void setEnabled(boolean enabled) {
		super.setEnabled(enabled);
		jCheckBoxLadderGame.setEnabled(enabled);
		jCheckBoxObservers.setEnabled(enabled);
		jCheckBoxPublic.setEnabled(enabled);
		switch (context.getGameState()) {
			case mpSelecting:
				jTreeLevels.setEnabled(true);
				jScrollPaneLevels.setVisible(true);
				jButton_new.setVisible(false);
				jButton_start.setVisible(false);
				jPanelMpOptions.setVisible(true);
				jCheckBoxReady.setVisible(true);
				break;
			case mpClientSelecting:
				jTreeLevels.setEnabled(false);
				jScrollPaneLevels.setVisible(false);
				jButton_new.setVisible(false);
				jButton_start.setVisible(false);
				jPanelMpOptions.setVisible(true);
				jCheckBoxReady.setVisible(true);
				break;
			default:
				jTreeLevels.setEnabled(true);
				jScrollPaneLevels.setVisible(true);
				jButton_new.setVisible(true);
				jButton_start.setVisible(true);
				jPanelMpOptions.setVisible(false);
				jCheckBoxReady.setVisible(false);
		}
	}

	private void refillList() {
		if (context != null) {
			ArrayList<MapInfo> wantedLevels;
			if (context.getGameState() != HexTD.GameState.mpSelecting) {
				wantedLevels = levelsSp;
			} else {
				wantedLevels = this.levelsMp;
			}
			if (levels != wantedLevels) {
				loading = true;
				levels = wantedLevels;
				nodes.clear();

				DefaultMutableTreeNode root = new DefaultMutableTreeNode("root");
				HashMap<Integer, DefaultMutableTreeNode> catNodes = new HashMap<Integer, DefaultMutableTreeNode>();
				List<Integer> keyList = new ArrayList<Integer>();
				for (MapInfo info : levels) {
					Integer catKey = new Integer(info.category);
					DefaultMutableTreeNode catNode = catNodes.get(catKey);
					if (catNode == null) {
						catNode = new DefaultMutableTreeNode(mapLoader.getCatName(catKey));
						catNodes.put(catKey, catNode);
						keyList.add(catKey);
					}
					DefaultMutableTreeNode treeNode = new DefaultMutableTreeNode(info);
					catNode.add(treeNode);
					nodes.put(info, treeNode);
				}
				Collections.sort(keyList);
				for (Integer key : keyList) {
					root.add(catNodes.get(key));
				}
				DefaultTreeModel model = (DefaultTreeModel) jTreeLevels.getModel();
				model.setRoot(root);

				loading = false;
			}

			switch (context.getGameState()) {
				case spSelecting:
				case mpSelecting:
					if (nodes.containsKey(context.loadedMapInfo)) {
						TreeNode node = nodes.get(context.loadedMapInfo);
						TreeNode parent = node.getParent();
						TreeNode root = parent.getParent();
						Object[] path = {root, parent, node};
						TreePath treePath = new TreePath(path);
						jTreeLevels.setSelectionPath(treePath);
					}
			}
		}
	}

	private void selectLevel(MapInfo map) {
		if (!loading) {
			PlayerInfo localPlayerInfo = context.getNetwork().getLocalPlayerInfo();
			if (map.havePreReq(localPlayerInfo)) {
				jButton_start.setEnabled(true);
				if (map.getContent() != null) {
					System.out.println("PanelLevelSelector::selectLevel: Loading cached map: " + map.mapId + " " + map.title);
					context.loadMap(map, null);
				} else {
					System.out.println("PanelLevelSelector::selectLevel: Waiting for map to load from server: " + map.mapId + " " + map.title);
					waitingForMap = map.mapId;
					mapLoader.getMap(map.mapId);
				}
			} else {
				jButton_start.setEnabled(false);
				context.setGameResult(HexTD.GameResult.badMap);
			}
		}
	}

	public void loadIndex(List<MapInfo> mapList) {
		loading = true;
		levelsSp.clear();
		levelsMp.clear();
		int sp = 0;
		int mp = 0;
		for (MapInfo info : mapList) {
			if (info.players > 1) {
				levelsMp.add(info);
				mp++;
			} else {
				levelsSp.add(info);
				sp++;
			}
		}
		System.out.println("PanelLevelSelector::loadIndex: Loaded " + mp + " " + sp);
		loading = false;
		levels = null;
		refillList();
	}

	private void loadNew() {
		MapInfo customMap = mapLoader.parseCustomMap(jTextPane1.getText());
		GameMap mapData = customMap.getMapData();
		if (mapData != null) {
			List<String> gridData = mapData.getGridData();
			if (gridData != null && gridData.size() > 0) {
				context.loadMap(customMap, "");
			}
		}
	}

	public void setLevelInfo(String html) {
		jTextPane_levelInfo.setText(html);
	}

	@Override
	public void mapIndexLoaded(List<MapInfo> index) {
		loadIndex(index);
	}

	@Override
	public void mapLoaded(MapInfo map) {
		if (waitingForMap == map.mapId) {
			selectLevel(map);
		}
	}

	@Override
	public void settingChanged(MessageSettings.settingKey key, String val) {
		switch (key) {
			case ladderGame:
				jCheckBoxLadderGame.setSelected(Boolean.valueOf(val));
				break;

			case openGame:
				jCheckBoxObservers.setSelected(Boolean.valueOf(val));
				break;

			case publicGame:
				jCheckBoxPublic.setSelected(Boolean.valueOf(val));
				break;
			case ready:
				boolean ready = Boolean.valueOf(val);
				if (jCheckBoxReady.isSelected() != ready) {
					jCheckBoxReady.setSelected(ready);
					sendSetting(MessageSettings.settingKey.ready, jCheckBoxReady.isSelected());
				}
				break;
		}
	}

	private void sendSetting(MessageSettings.settingKey setting, boolean value) {
		HashMap<MessageSettings.settingKey, String> transmit = new HashMap<MessageSettings.settingKey, String>();
		transmit.put(setting, Boolean.toString(value));
		String msg = MessageSettings.formatMessage(0, transmit);
		context.messageSend(msg);
	}

	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jFrame1 = new javax.swing.JFrame();
        jPanel1 = new javax.swing.JPanel();
        jButton_cancel = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTextPane1 = new javax.swing.JTextPane();
        jButton_load = new javax.swing.JButton();
        jScrollPaneLevels = new javax.swing.JScrollPane();
        jTreeLevels = new javax.swing.JTree();
        jButton_start = new javax.swing.JButton();
        jButton_new = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        jTextPane_levelInfo = new javax.swing.JTextPane();
        jPanelMpOptions = new javax.swing.JPanel();
        jCheckBoxObservers = new javax.swing.JCheckBox();
        jCheckBoxLadderGame = new javax.swing.JCheckBox();
        jCheckBoxPublic = new javax.swing.JCheckBox();
        jCheckBoxReady = new javax.swing.JCheckBox();

        jFrame1.setBackground(new java.awt.Color(0, 0, 0));
        jFrame1.setMinimumSize(new java.awt.Dimension(600, 400));

        jPanel1.setBackground(new java.awt.Color(0, 0, 0));
        jPanel1.setForeground(new java.awt.Color(220, 255, 220));
        jPanel1.setLayout(new java.awt.GridBagLayout());

        jButton_cancel.setBackground(new java.awt.Color(0, 0, 0));
        jButton_cancel.setForeground(new java.awt.Color(220, 255, 220));
        jButton_cancel.setText("Cancel");
        jButton_cancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_cancelActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTHWEST;
        jPanel1.add(jButton_cancel, gridBagConstraints);

        jLabel1.setBackground(new java.awt.Color(0, 0, 0));
        jLabel1.setForeground(new java.awt.Color(220, 255, 220));
        jLabel1.setText("Paste the level definition in the box and click \"Load\".");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridwidth = 2;
        jPanel1.add(jLabel1, gridBagConstraints);

        jScrollPane2.setViewportView(jTextPane1);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 0.01;
        gridBagConstraints.weighty = 0.01;
        jPanel1.add(jScrollPane2, gridBagConstraints);

        jButton_load.setBackground(new java.awt.Color(0, 0, 0));
        jButton_load.setForeground(new java.awt.Color(220, 255, 220));
        jButton_load.setText("Load");
        jButton_load.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_loadActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTHEAST;
        jPanel1.add(jButton_load, gridBagConstraints);

        jFrame1.getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);

        setBackground(context.colors.getColorBackGround());
        setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Select Level", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("DejaVu Sans", 0, 12), context.colors.getColorForeGround())); // NOI18N
        setForeground(context.colors.getColorForeGround());
        setMaximumSize(new java.awt.Dimension(200, 2147483647));
        setMinimumSize(new java.awt.Dimension(200, 25));
        setPreferredSize(new java.awt.Dimension(200, 139));
        setLayout(new java.awt.GridBagLayout());

        jScrollPaneLevels.setBackground(context.colors.getColorBackGround());
        jScrollPaneLevels.setForeground(context.colors.getColorForeGround());

        jTreeLevels.setBackground(context.colors.getColorBackGround());
        jTreeLevels.setForeground(context.colors.getColorForeGround());
        javax.swing.tree.DefaultMutableTreeNode treeNode1 = new javax.swing.tree.DefaultMutableTreeNode("JTree");
        jTreeLevels.setModel(new javax.swing.tree.DefaultTreeModel(treeNode1));
        jTreeLevels.setRootVisible(false);
        jTreeLevels.setScrollsOnExpand(true);
        jTreeLevels.setToggleClickCount(1);
        jTreeLevels.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener() {
            public void valueChanged(javax.swing.event.TreeSelectionEvent evt) {
                jTreeLevelsValueChanged(evt);
            }
        });
        jScrollPaneLevels.setViewportView(jTreeLevels);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0E-4;
        gridBagConstraints.weighty = 0.1;
        add(jScrollPaneLevels, gridBagConstraints);

        jButton_start.setBackground(context.colors.getColorBackGround());
        jButton_start.setForeground(context.colors.getColorForeGround());
        jButton_start.setText("Start!");
        jButton_start.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_startActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 0.01;
        gridBagConstraints.insets = new java.awt.Insets(10, 5, 10, 5);
        add(jButton_start, gridBagConstraints);

        jButton_new.setBackground(context.colors.getColorBackGround());
        jButton_new.setForeground(context.colors.getColorForeGround());
        jButton_new.setText("New");
        jButton_new.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_newActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 0.01;
        gridBagConstraints.insets = new java.awt.Insets(10, 5, 10, 5);
        add(jButton_new, gridBagConstraints);

        jPanel2.setBackground(context.colors.getColorBackGround());
        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Level Info", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("DejaVu Sans", 0, 12), context.colors.getColorForeGround())); // NOI18N
        jPanel2.setForeground(context.colors.getColorForeGround());
        jPanel2.setLayout(new java.awt.BorderLayout());

        jScrollPane3.setBorder(null);

        jTextPane_levelInfo.setBorder(null);
        jScrollPane3.setViewportView(jTextPane_levelInfo);

        jPanel2.add(jScrollPane3, java.awt.BorderLayout.CENTER);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0E-4;
        gridBagConstraints.weighty = 0.05;
        add(jPanel2, gridBagConstraints);

        jPanelMpOptions.setBackground(context.colors.getColorBackGround());
        jPanelMpOptions.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Options", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("DejaVu Sans", 0, 12), context.colors.getColorForeGround())); // NOI18N
        jPanelMpOptions.setForeground(context.colors.getColorForeGround());
        jPanelMpOptions.setLayout(new java.awt.GridBagLayout());

        jCheckBoxObservers.setBackground(context.colors.getColorBackGround());
        jCheckBoxObservers.setForeground(context.colors.getColorForeGround());
        jCheckBoxObservers.setSelected(true);
        jCheckBoxObservers.setText("Allow Observers");
        jCheckBoxObservers.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCheckBoxObserversActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 5, 0, 5);
        jPanelMpOptions.add(jCheckBoxObservers, gridBagConstraints);

        jCheckBoxLadderGame.setBackground(context.colors.getColorBackGround());
        jCheckBoxLadderGame.setForeground(context.colors.getColorForeGround());
        jCheckBoxLadderGame.setText("Ladder Game");
        jCheckBoxLadderGame.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCheckBoxLadderGameActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 5, 0, 5);
        jPanelMpOptions.add(jCheckBoxLadderGame, gridBagConstraints);

        jCheckBoxPublic.setBackground(context.colors.getColorBackGround());
        jCheckBoxPublic.setForeground(context.colors.getColorForeGround());
        jCheckBoxPublic.setSelected(true);
        jCheckBoxPublic.setText("Public Replay");
        jCheckBoxPublic.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCheckBoxPublicActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 5, 0, 5);
        jPanelMpOptions.add(jCheckBoxPublic, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        add(jPanelMpOptions, gridBagConstraints);

        jCheckBoxReady.setBackground(context.colors.getColorBackGround());
        jCheckBoxReady.setForeground(context.colors.getColorForeGround());
        jCheckBoxReady.setText("I'm Ready!");
        jCheckBoxReady.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCheckBoxReadyActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.insets = new java.awt.Insets(10, 5, 10, 5);
        add(jCheckBoxReady, gridBagConstraints);
    }// </editor-fold>//GEN-END:initComponents

    private void jButton_loadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_loadActionPerformed
		this.loadNew();
		this.jFrame1.setVisible(false);
    }//GEN-LAST:event_jButton_loadActionPerformed

    private void jButton_cancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_cancelActionPerformed
		this.jFrame1.setVisible(false);
    }//GEN-LAST:event_jButton_cancelActionPerformed

    private void jButton_newActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_newActionPerformed
		this.jFrame1.pack();
		this.jFrame1.setVisible(true);
    }//GEN-LAST:event_jButton_newActionPerformed

    private void jButton_startActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_startActionPerformed
		this.context.cleanContext();
		this.mainApp.startLevel(true);
    }//GEN-LAST:event_jButton_startActionPerformed

	private void jCheckBoxObserversActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCheckBoxObserversActionPerformed
		sendSetting(MessageSettings.settingKey.openGame, jCheckBoxObservers.isSelected());
	}//GEN-LAST:event_jCheckBoxObserversActionPerformed

	private void jCheckBoxPublicActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCheckBoxPublicActionPerformed
		sendSetting(MessageSettings.settingKey.publicGame, jCheckBoxPublic.isSelected());
	}//GEN-LAST:event_jCheckBoxPublicActionPerformed

	private void jCheckBoxLadderGameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCheckBoxLadderGameActionPerformed
		sendSetting(MessageSettings.settingKey.ladderGame, jCheckBoxLadderGame.isSelected());
	}//GEN-LAST:event_jCheckBoxLadderGameActionPerformed

	private void jCheckBoxReadyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCheckBoxReadyActionPerformed
		sendSetting(MessageSettings.settingKey.ready, jCheckBoxReady.isSelected());
	}//GEN-LAST:event_jCheckBoxReadyActionPerformed

	private void jTreeLevelsValueChanged(javax.swing.event.TreeSelectionEvent evt) {//GEN-FIRST:event_jTreeLevelsValueChanged
		TreePath selectionPath = jTreeLevels.getSelectionPath();
		if (selectionPath != null) {
			DefaultMutableTreeNode lastPathComponent = (DefaultMutableTreeNode) selectionPath.getLastPathComponent();
			Object userObject = lastPathComponent.getUserObject();
			if (userObject instanceof MapInfo) {
				selectLevel((MapInfo) userObject);
			}
		}
	}//GEN-LAST:event_jTreeLevelsValueChanged
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton_cancel;
    private javax.swing.JButton jButton_load;
    private javax.swing.JButton jButton_new;
    private javax.swing.JButton jButton_start;
    private javax.swing.JCheckBox jCheckBoxLadderGame;
    private javax.swing.JCheckBox jCheckBoxObservers;
    private javax.swing.JCheckBox jCheckBoxPublic;
    private javax.swing.JCheckBox jCheckBoxReady;
    private javax.swing.JFrame jFrame1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanelMpOptions;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPaneLevels;
    private javax.swing.JTextPane jTextPane1;
    private javax.swing.JTextPane jTextPane_levelInfo;
    private javax.swing.JTree jTreeLevels;
    // End of variables declaration//GEN-END:variables
}
